﻿using DiffPlex;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp2.Util;
using Emgu.CV.UI;
using Emgu.CV;
using Emgu.CV.Structure;

namespace WindowsFormsApp2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        string pathElec;
        string pathScan;
       

        private void splitContainer2_Panel1_Click(object sender, EventArgs e)
        {
            //添加原文件
            string path = DialogHelper.ShowOpenImageFile();
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            pathElec = path;

         
            imageBox1.Image = new Image<Bgr, Byte>(pathElec);
            
        
        }

        private void splitContainer3_Panel1_Click(object sender, EventArgs e)
        {
            //添加对比文件
            string path = DialogHelper.ShowOpenImageFile();
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            pathScan = path;

            imageBox2.Image = new Image<Bgr, Byte>(pathScan);

        }


        string leftXml = "left.xml";
        string rightXml = "right.xml";
        private void 对比ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            //Hello.HelloForm he = new Hello.HelloForm();
            //he.loadEngine();
            //he.processImage(pathElec, leftXml);
            //he.processImage(pathScan,rightXml);
            //he.unloadEngine();



            //string resultLeft = DealYDOCR.GetText(pathElec);
            //textBox4.Text = resultLeft;
            //string resultRight = DealYDOCR.GetText(pathScan);
            //string resultLeftText = DealYDOCR.DealText(resultLeft);
            //string resultRightText = DealYDOCR.DealText(resultRight);

            //string conclusionText = DiffConclusion(resultLeftText,resultLeftText);

            ////测试用 
            //textBox1.Text = resultLeftText;
            //textBox2.Text = resultRightText;
            //textBox3.Text = DiffConclusion(textBox1.Text, textBox2.Text);
        }

        private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        public string  DiffConclusion(string oldtext, string newtext)
        {
            string conclusionText = "";
            var diffBuilder = new InlineDiffBuilder(new Differ());

            DiffPaneModel diff = diffBuilder.BuildDiffModel(oldtext   , newtext);//在这里做了识别行

            var lastposition =0;
            foreach (var line in diff.Lines)
            {
                
                switch (line.Type)
                {
                    
                    case ChangeType.Inserted:
                        lastposition = Convert.ToInt16(line.Position);
                        conclusionText += line.Position+"新增 ";
                        conclusionText += line.Text + "\r\n";
                        break;
                    case ChangeType.Deleted:
                        conclusionText += lastposition + "删除 ";
                        conclusionText += line.Text + "\r\n";
                        break;
                    case ChangeType.Modified:
                        lastposition = Convert.ToInt16(line.Position);
                        conclusionText += line.Position + "被修改 ";
                        conclusionText += line.Text + "\r\n";
                        break;
                    default:
                        lastposition = Convert.ToInt16(line.Position);
                        conclusionText += "";
                        break;
                        
                }

                
            }
            return conclusionText;
        }

        private void 测试差异ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox3.Text = DiffConclusion(textBox1.Text, textBox2.Text);
        }

        private void imageBox1_Click(object sender, EventArgs e)
        {
            splitContainer2_Panel1_Click(sender, e);
        }

        private void imageBox2_Click(object sender, EventArgs e)
        {
            splitContainer3_Panel1_Click(sender, e);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }

   
}
